/*
 * Takeing.java
 *
 * Created on __DATE__, __TIME__
 */

package WindowsModule;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;

import Client.ClientListener;
import Client.MsgEvent;
import Windows.MainWindows;
import XML.XML;

/**
 * 
 * @author __USER__
 */
public class Talking extends javax.swing.JFrame implements MessageWindowsRevListener, ClientListener {
	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				new Talking("").setVisible(true);
			}
		});
	}
	private String userid;

	private UserInfoTakeing ust;

	// GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JButton jButton1;

	private javax.swing.JButton jButton2;

	private javax.swing.JButton jButton3;

	private javax.swing.JLabel jLabel1;

	// GEN-END:initComponents

	private javax.swing.JLabel jLabel2;

	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JScrollPane jScrollPane2;
	private javax.swing.JTextArea jTextArea1;
	private javax.swing.JTextArea jTextArea2;
	/** Creates new form Takeing */
	public Talking(String id) {
		userid = id;

		init();

		MainWindows.getInstance().client.send("<talk userid=\"" + id + "\" />");
	}
	public Talking(XML cmd) {
		init();

		userid = cmd.get("userid");
		ust = new UserInfoTakeing(cmd.get("userid"));
		ust.setName(cmd.get("name"));
		ust.setSex(cmd.get("sex"));
		ust.setAddress(cmd.get("address"));
		ust.setEmail(cmd.get("email"));

		jLabel2.setText(ust.getName() + "(在线)");
		UserInfoTakeing u = ust;
		StringBuffer bf = new StringBuffer("昵称：");
		bf.append(u.getName());
		bf.append("\n性别：");
		bf.append(u.getSex());
		bf.append("\n地址：");
		bf.append(u.getAddress());
		bf.append("\n邮箱：");
		bf.append(u.getEmail());

		jLabel1.setText(bf.toString());

	}
	@Override
	public void dispose() {
		MainWindows.getInstance().client.removeClientListenser(this);
		super.dispose();
	}
	@Override
	public void fettleChange(String userId, int flag) {
		if (userId.equals(userid))
			if (flag == 0) {
				jLabel2.setText("在线");
			} else if (flag == 1) {
				jLabel2.setText("离开");
			} else if (flag == 2) {
				jLabel2.setText("隐身");
			} else {
				jLabel2.setText("下线");
			}

	}
	private void init() {
		initComponents();
		MainWindows.getInstance().client.addClientListenser(this);

		jButton1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (jTextArea2.getText().length() > 0) {
					MainWindows.getInstance().client.send("<talkmsg userid=\"" + userid + "\" >" + jTextArea2.getText() + "</talkmsg>");
					jTextArea1.setText(jTextArea1.getText() + "\r\n" + MainWindows.getInstance().client.userid + ":\r\n" + jTextArea2.getText() + "\r\n");
					jTextArea2.setText("");
				}
			}
		});
		jButton3.setVisible(false);
		jButton2.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
	}

	// End of variables declaration//GEN-END:variables

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jScrollPane1 = new javax.swing.JScrollPane();
		jTextArea1 = new javax.swing.JTextArea();
		jScrollPane2 = new javax.swing.JScrollPane();
		jTextArea2 = new javax.swing.JTextArea();
		jButton1 = new javax.swing.JButton();
		jButton2 = new javax.swing.JButton();
		jLabel1 = new javax.swing.JLabel();
		jButton3 = new javax.swing.JButton();
		jLabel2 = new javax.swing.JLabel();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

		jTextArea1.setColumns(20);
		jTextArea1.setEditable(false);
		jTextArea1.setRows(7);
		jScrollPane1.setViewportView(jTextArea1);

		jTextArea2.setColumns(20);
		jTextArea2.setRows(5);
		jScrollPane2.setViewportView(jTextArea2);

		jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/javax/swing/plaf/metal/icons/ocean/upFolder.gif"))); // NOI18N
		jButton1.setText("\u53d1\u9001");

		jButton2.setText("\u5173\u95ed");

		jLabel1.setText("jLabel1");

		jButton3.setText("\u804a\u5929\u8bb0\u5f55");

		jLabel2.setText("jLabel2");

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup()
						.addContainerGap()
						.addGroup(
								layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 419, Short.MAX_VALUE).addContainerGap()).addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addComponent(jButton1).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(jButton3).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jButton2).addGap(170, 170, 170))
										.addGroup(layout.createSequentialGroup().addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 302, Short.MAX_VALUE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE).addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)).addContainerGap()))));
		layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup().addContainerGap().addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 285, Short.MAX_VALUE).addGroup(layout.createSequentialGroup().addGap(8, 8, 8).addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 225, javax.swing.GroupLayout.PREFERRED_SIZE))).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jButton3).addComponent(jButton1).addComponent(jButton2)).addGap(42, 42, 42)));

		pack();
	}// </editor-fold>

	@Override
	public void msgRev(String userId, String msg, Date time) {
		// TODO Auto-generated method stub
		StringBuffer sb = new StringBuffer();
		sb.append(userId);
		sb.append("  ");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		sb.append(sdf.format(time));
		sb.append("\n");
		sb.append(msg);

		jTextArea1.append(sb.toString());

	}

	@Override
	public void onCloseEvent(Client.CloseEvent cce) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onMsgEvent(MsgEvent ce) {
		if (ce.getCmd().FLAG.equals("talk")) {
			XML cmd = ce.getCmd();
			ust = new UserInfoTakeing(cmd.get("userid"));
			ust.setName(cmd.get("name"));
			ust.setSex(cmd.get("sex"));
			ust.setAddress(cmd.get("address"));
			ust.setEmail(cmd.get("email"));

			jLabel2.setText(ust.getName() + "(在线)");
			jLabel1.setText("");

			UserInfoTakeing u = ust;
			StringBuffer bf = new StringBuffer("昵称：");
			bf.append(u.getName());
			bf.append("\r\n性别：");
			bf.append(u.getSex());
			bf.append("\r\n地址：");
			bf.append(u.getAddress());
			bf.append("\r\n邮箱：");
			bf.append(u.getEmail());
			bf.append("\r\n");

			jTextArea1.setText(bf.toString());

			setVisible(true);
		} else if (ce.getCmd().FLAG.equals("talkmsg")) {
			XML cmd = ce.getCmd();
			if (cmd.get("userid").equals(userid)) {
				jTextArea1.setText(jTextArea1.getText() + "\r\n" + cmd.get("userid") + ":\r\n" + cmd.getData() + "\r\n");
			}
		}
	}

	@Override
	public void sysMsg(String msg) {
		// TODO Auto-generated method stub

	}

}